<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\WechatMiniProgramLoginRequest;
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use EasyWeChat\MiniProgram\Application;
use App\Http\Resources\Token;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

/**
 * Class LoginController
 * @package App\Http\Controllers\Api@resource 登录
 *
 */
class LoginController extends Controller
{
    use AuthenticatesUsers;

    /**
     * 微信小程序登录
     *
     * @response {"data": {"token": "jxqqagg4ykN20k8ZCUFueXfkimlIiyFyuqPP2AQFQFOkXc4m5LOxXWhgTvSF"}}
     * @param WechatMiniProgramLoginRequest $request
     * @param UserRepository $userRepository
     * @param Application $application
     * @return Token
     */
    public function wechatMiniProgram(WechatMiniProgramLoginRequest $request,
                                      UserRepository $userRepository,
                                      Application $application)
    {
        $session = $application->auth->session($request->code);
        return new Token($userRepository->attemptWechatLogin($session['openid']));
    }
}